<?php

namespace App\Model\Backend;

use Illuminate\Database\Eloquent\Model;

class QiyehaoModel extends Model
{
    protected $table = 'qiyehao';
    protected $primaryKey = 'id';
    protected $corpid = 'wxafd8c7043b4c0e3e';
    protected $corpsecret = 'jrVYVl3DKqXvblmgcZvbkkzWCwECH1SvuZSXpzFPkZ7QI2p4ZWtjy5yJOE7WE5xB';
    protected $access_token;


    public function __construct()
    {
        if(session('access_token')){
            $this->access_token=session('access_token');
        }else{
            $url_gettoken = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $this->corpid . '&corpsecret=' . $this->corpsecret;
            $a= json_decode(file_get_contents($url_gettoken), true);
            $this->access_token=$a["access_token"];
            session(['access_token'=>$a["access_token"]]);
        }


    }
    public function text($userID,$content){
        $url_send = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' . $this->access_token;
        $body = '{
                 "touser": "' . $userID . '",
                 "toparty": "",
                 "totag": "",
                 "msgtype": "text",
                "agentid": "0",
                "text": {
                     "content":" ' . $content . '"
                 },
                 "safe":"0"
                 }';
        $timeout = 30;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_send);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // �
        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // ��������
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //���ݹ�������
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        $return=curl_exec($ch);
        curl_close($ch);
        return $return;

    }

}
